﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using BattleEngineCommon;
using MarvinsArena.Core;

namespace BattleEngine2D
{
	public class BattleEngine: IGame
	{
        public IHudMessages hudMessages { get; set; }
        public IMap map { get; set; }
        public ICamera camera { get; set; }
        public List<IProjectile> projectiles { get; set; }
        public List<IRobot> robots { get; set; }

        private Hud hudEngine;
        private Camera cameraEngine;
		private Map mapEngine;
        private SpriteBatch spriteBatch;
        private GraphicsDeviceManager graphics;
        private ContentManager content;

        private Texture2D robotTexture;

        private Tournament tournament;

		public BattleEngine(GraphicsDeviceManager Graphics, ContentManager Content, Tournament tournament)
		{
			this.graphics = Graphics;
			this.content = Content;

            this.tournament = tournament;

            cameraEngine = new Camera();
            mapEngine = new Map();
            hudEngine = new Hud(graphics.PreferredBackBufferWidth);
		}

        public void Initialize(SpriteBatch spriteBatch)
        {
            this.spriteBatch = spriteBatch;
            this.mapEngine = new Map();

            //Initialize camera
            cameraEngine.Initialize(graphics);
        }

        public void LoadContent()
		{
            //Loading map content
			mapEngine.LoadContent(content);

            //Loading HUD's content
            hudEngine.LoadContent(content);

            //Loading robot content
            this.robotTexture = content.Load<Texture2D>(@"Robots\2D\Robot");
        }

        public void Draw(GameTime gameTime)
		{

			this.BeforeDrawing();
			
			//Drawing map
			mapEngine.Draw(this.spriteBatch, cameraEngine);

            //Drawing robot's
            foreach (IRobot robot in robots)
            {
                Robot r = new Robot(robot);
                r.Draw(this.spriteBatch, robotTexture, cameraEngine);
            }

            //Drawing HUD's
            //Select current robot
            IRobot target = cameraEngine.TargetIndex == cameraEngine.EmptyTarget ? null : robots[cameraEngine.TargetIndex];
            hudEngine.Draw(this.spriteBatch, target, robots, map, tournament.Rules);
		}

		public void BeforeDrawing()
		{
			//Update map
			this.mapEngine.Heightmap = this.map.Heightmap;

			//Updating camera position
			this.cameraEngine.isMoveLeft = this.camera.isMoveLeft;
			this.cameraEngine.isMoveRight = this.camera.isMoveRight;
			this.cameraEngine.isMoveUp = this.camera.isMoveUp;
            this.cameraEngine.isMoveDown = this.camera.isMoveDown;
			this.cameraEngine.TargetIndex = this.camera.TargetIndex;
            this.cameraEngine.MoveOnDefaultPosition = this.camera.MoveOnDefaultPosition;
            //Updating camera
			this.cameraEngine.UpdateCamera(robots, mapEngine);

            //Updating HUD's
            this.hudEngine.messages.ListMessages = this.hudMessages.ListMessages;
            this.hudEngine.messages.StatusMessage = this.hudMessages.StatusMessage;
            this.hudEngine.messages.isStatusMessageActive = this.hudMessages.isStatusMessageActive;
		}

        public void UnloadContent()
		{
		}
	}
}